//
//  ShopUserViewController.m
//  EasyToTesco
//
//  Created by mc on 16/3/11.
//  Copyright © 2016年 Jack. All rights reserved.
//

#import "ShopUserViewController.h"
#import "HeaderFileForEasyToTesco.h"
#import "AFNetworking.h"

#import "shopuserMoneyViewController.h"
#import "shopuserTextViewController.h"
#import "shopuserUNViewController.h"
#import "shopuserMoneyListViewController.h"

@interface ShopUserViewController ()<UITableViewDataSource, UITableViewDelegate>
{
  
}

@property (nonatomic, strong)UITableView *shopUserTableview;


@end

@implementation ShopUserViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    
    
    [self addshopUserTopview];
    
    [self addshopusertableviewUI];
    
    
    
}

- (void)addshopUserTopview {
    
    UIImageView *image1 = [[UIImageView alloc] initWithFrame:CGRectMake(10, (self.secondView.frame.size.height-25)/2, 20, 20)];
    image1.image = [UIImage imageNamed:@"goback-icon.png"];
    [self.secondView addSubview:image1];
    self.leftBtn.hidden = YES;
    
    UIButton *goBackBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    goBackBtn.frame = CGRectMake(0, 0, self.secondView.frame.size.height, self.secondView.frame.size.height);
    [goBackBtn addTarget:self action:@selector(gobackClick) forControlEvents:UIControlEventTouchUpInside];
    [self.secondView addSubview:goBackBtn];
    self.titleLabel.text = @"商家服务";
    
}
- (void)gobackClick {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)addshopusertableviewUI {
    self.shopUserTableview = [[UITableView alloc] initWithFrame:CGRectMake(0,self.secondView.frame.size.height + 13, WEITH, HIGHT) style:UITableViewStyleGrouped];
    self.shopUserTableview.delegate = self;
    self.shopUserTableview.dataSource = self;
    self.shopUserTableview.rowHeight = 50;
    self.shopUserTableview.tableFooterView = [[UIView alloc] init];
    self.shopUserTableview.contentInset = UIEdgeInsetsMake(-35, 0, 0, 0);
//    self.shopUserTableview.bounces = NO;
    [self.view addSubview:self.shopUserTableview];
}

#pragma mark -- UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 4;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"shopusercell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
    if (indexPath.row == 0) {
        cell.textLabel.text = @"商家合作专区";
    }else if (indexPath.row == 1) {
        cell.textLabel.text = @"合作记录";
    }else if (indexPath.row == 2){
        cell.textLabel.text = @"营业额结算";
    } else {
        cell.textLabel.text = @"营业款结算列表";
    }
    
        return cell;
}

#pragma mark -- UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        shopuserUNViewController *UNVC = [[shopuserUNViewController alloc] init];
        [self presentViewController:UNVC animated:YES completion:nil];
    } else if (indexPath.row == 1) {
        shopuserTextViewController *TextVc = [[shopuserTextViewController alloc] init];
        [self presentViewController:TextVc animated:YES completion:nil];
    } else if (indexPath.row == 2){
        
        shopuserMoneyViewController *moneyVc = [[shopuserMoneyViewController alloc] init];
        
        [self presentViewController:moneyVc animated:YES completion:nil];
    } else {
        shopuserMoneyListViewController *shopuserList = [[shopuserMoneyListViewController alloc] init];
        
        [self presentViewController:shopuserList animated:YES completion:nil];
    }
    
}



//shopusermoney
- (void)shopUserMoneyDatasources{
    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
    mgr.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    NSString *username = @"18620614920";
    NSString *jine = @"10";
    
    NSDictionary *param = @{@"username":[NSString stringWithFormat:@"%@", username], @"jine":[NSString stringWithFormat:@"%@", jine]};
    
    [mgr POST:@"http://www.ylgw8.com/AppHz/Jies" parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
//        NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        NSDictionary *dic = [self toArrayOrNSDictionary:responseObject];
        
        NSLog(@"%@",dic);
        
        NSString *diccode = [NSString stringWithFormat:@"%@", dic[@"code"]];
        
        if ([diccode isEqualToString:@"101"]) {
            UIAlertView *alview = [[UIAlertView alloc] initWithTitle:@"提示" message:[NSString stringWithFormat:@"%@", dic[@"message"]] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alview show];
        } else {
        
        UIAlertView *alview = [[UIAlertView alloc] initWithTitle:@"提示" message:[NSString stringWithFormat:@"%@", dic] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alview show];
        }
        
        
        NSLog(@"%@", dic);
        [self.shopUserTableview reloadData];
    } failure:^(AFHTTPRequestOperation *operation,NSError *error)
     {
         //         operation.response.statusCode;//404 401之类
         //         NSLog(@"数据请求错误原因:%@", error);
         //         NSLog(@"数据请求错误码:%ld", (long)operation.response.statusCode);
     }];
}
// 将JSON串转化为字典或者数组
- (id)toArrayOrNSDictionary:(NSData *)jsonData{
    NSError *error = nil;
    id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData
                                                    options:NSJSONReadingAllowFragments
                                                      error:&error];
    
    if (jsonObject != nil && error == nil){
        return jsonObject;
    }else{
        // 解析错误
        return nil;
    }
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
